<?php
$title="HTML Markdown 孵化器";
include 'header.php';
if (isset($_POST['model']) && $_POST['model']=='h2m' && !empty($_POST['xhtml'])) {
    include_once('libs/markdownify/markdownify_extra.php');
    if (!isset($_POST['leap'])) {
        $leap = MDFY_LINKS_EACH_PARAGRAPH;
    } else {
        $leap = $_POST['leap'];
    }

    if (!isset($_POST['keepHTML'])) {
        $keephtml = MDFY_KEEPHTML;
    } else {
        $keephtml = $_POST['keepHTML'];
    }

    if (!empty($_POST['extra'])) {
        $md = new Markdownify_Extra($leap, MDFY_BODYWIDTH, $keephtml);
    } else {
        $md = new Markdownify($leap, MDFY_BODYWIDTH, $keephtml);
    }

    if (ini_get('magic_quotes_gpc')) {
        $_POST['xhtml'] = stripslashes($_POST['xhtml']);
    }

    $markdown = $md->parseString($_POST['xhtml']);
    $xhtml = $_POST['xhtml'];
}elseif(isset($_POST['model']) && $_POST['model']=='m2h' && !empty($_POST['markdown'])){
    include_once '../site/helpers/markdown_helper.php';
    $markdown = isset($_POST['markdown'])?$_POST['markdown']:'';
    $xhtml=Markdown($markdown);
} else {
    $xhtml='';
    $markdown='';
}
$markdown = htmlspecialchars($markdown, ENT_NOQUOTES, 'UTF-8');
$xhtml = htmlspecialchars($xhtml, ENT_NOQUOTES, 'UTF-8');
?>

<div class='clear'></div>
<style type="text/css" media="screen">
    h2.small{font-size:16px;margin:8px 0px;line-height:5px;}
</style>
<div style="float:left;width:49%;">
    <h2 class='small'>HTML To Markdown </h2>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='h2m'>
        HTML Source&gt;&gt;<br/>
        <textarea style="width:100%;" name="xhtml"rows="25" cols="80"><?=$xhtml?></textarea>
        <div class='form-inline' style="text-align:right;">
            <label for="extra">Markdownify Extra: <input name="extra" checked="checked" id="extra" type="checkbox" value="1"  /></label>
            <label for="leap">Links after each block elem: <input name="leap" id="leap" type="checkbox" value="1"  /></label>
            <label for="keepHTML">keep HTML: <input name="keepHTML" id="keepHTML" type="checkbox" value="1" checked="checked" /></label>
            <input type="hidden" name="model" value="h2m" id="model" />
            <input type="submit" name="submit" value="Convert==&gt;"  class="btn btn-success" />
        </div>
    </form>
</div>

<div style="float:left;width:49%;margin-left: 20px;">
    <h2 class='small'>Markdown To HTml </h2>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='m2h'>
    Markdown Source &gt;&gt;<br/>
    <textarea style="width:100%;"name="markdown"rows="25" cols="80"><?=$markdown?></textarea>
    <div class='form-inline'>
        <input type="submit" name="convert"  value="&lt;==Convert"  class="btn btn-success"/>
        <label>Results:&nbsp;</label> 
        <label><input type="radio" name="view" value="source">HTML源码</label>
        <label><input type="radio" name="view" value="preview">预览效果</label>
        <label><input type="radio" name="view" value="both" checked="checked">HTML源码 &amp; 预览</label>
        <input type="hidden" name="model" value="m2h" id="model" />
    </div>
</div>

<div class='clear'></div>
<script type="text/javascript" charset="utf-8">
    text = document.forms['m2h'].markdown.value
    text = text.replace(/<br>/g,'  ');
    text = text.replace(/<dfn>/g,'_');
    text = text.replace(/<\/dfn>/g,'_');
    text = text.replace(/<strong>/g,'**');
    text = text.replace(/<\/strong>/g,'**');
    text = text.replace(/(<div)(.)*/g,'');
    text = text.replace(/<\/div>/g,'');
    text = text.replace(/(<p)(.)*/g,'');
    text = text.replace(/<\/p>/g,'');
    text = text.replace(/<tr>/g,' ');
    text = text.replace(/<td(.)*>/g,'\t|');
    text = text.replace(/<th(.)*>/g,'\t|');
    text = text.replace(/<\/tr>/g,' ');
    text = text.replace(/<\/td>/g,' ');
    text = text.replace(/<\/th>/g,' ');
    text = text.replace(/(\t[|]\n)/g,'\t|');
    text = text.replace(/&nbsp;/g,' ');
    text = text.replace(/&gt;/g,'>');
    text = text.replace(/\r\n/g,"\n");
    text = text.replace(/^[ ]\n/g,"\n");
    text = text.replace(/^[ \t]+$/mg,"");
    text = text.replace(/^[ ]{0,3}/g,'');
    text = text.replace(/\n{3}/g,'\n');
    document.forms['m2h'].markdown.value = text;
</script>

<?php include 'footer.php'; ?>
